// 导入express模块
var express = require('express');
// 导入路由模块
var router = express.Router();
// 导入url模块
var url = require('url');
// 导入mysql模块 
var mysql = require('mysql');
// 导入数据库配置信息
var dbconfig = require('../db/DBConfig');
// 导入SQL查询语句
var task = require('../sql/Tasksql.js');
// 导入自定义公共类
var util = require('../utils/util');
// 使用DBConfig.js的配置信息创建一个MySQL连接池
var pool = mysql.createPool(dbconfig.mysql);

/* POST menu listing. */
/* 查询所有作业接口 */
router.post('/getall', function(req, res, next) {
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(task.queryALLData(), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "作业获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "作业获取失败", "code":400 });
            }
        });
    })
});
/* 根据用户ID查询作业接口 */
router.post('/getbyid', function(req, res, next) {
    let params = {
        uid: req.body.rid,
        sid: req.body.sid
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(task.queryIDData(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "作业获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": 0, "msg": "作业获取失败", "code":400 });
            }
        });
    })
});

/* 根据ID查询作业接口 */
router.post('/gettaskbyid', function(req, res, next) {
    let params = {
        id: req.body.id
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(task.queryBYIDData(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                results[0].imgpath = '../images/' + results[0].imgpath;
                res.send({ "success": true, "data": {results}, "msg": "作业获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": 0, "msg": "作业获取失败", "code":400 });
            }
        });
    })
});

/* 根据ID查询作业接口 */
router.post('/gettaskbyuid', function(req, res, next) {
    let params = {
        uid: req.body.uid
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(task.queryBYUIDData(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "作业获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": 0, "msg": "作业获取失败", "code":400 });
            }
        });
    })
});
/* 添加作业 */
router.post('/add', function(req, res, next) {
    // 获取请求字段
    let params = {
        title: req.body.title,
        content: req.body.content,
        imgpath: req.body.imgpath,
        subId: req.body.subId,
        tuid: req.body.tuid,
        adt:util.CurentDay()
    };

    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(task.insertData(params), function(err, results) {
            if (results.affectedRows!=0) {
                res.send({ "success": true, "data": {}, "msg": "添加成功" });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "添加失败" });
            }
        });
    });
});

/* 根据班级、学科查询作业 */
router.post('/queryTCData', function(req, res, next) {
    // 获取请求字段
    let params = {
        BID: req.body.CID,
        SID: req.body.SID
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(task.queryTCData(params), function(err, results) {
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "作业获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": 0, "msg": "作业获取失败", "code":400 });
            }
        });
    });
});


/* 删除班级 */
router.post('/del', function(req, res, next) {
    // 获取请求字段
    let params = {
        ID: req.body.ID
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(task.deleteData(params), function(err, results) {
            if (util.isEmpty(results)) {
                if (!util.isEmpty(results)) {
                    res.send({ "success": true, "data": {}, "msg": "删除成功" });
                } else {
                    res.send({ "success": false, "data": {}, "msg": "参数输入不合法" });
                }
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "删除失败" });
            }
        });
    });
});

/* 打分*/
router.post('/update', function(req, res, next) {
    // 获取请求字段
    let params = {
        ID: req.body.ID,
        achieve: req.body.achieve,
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(task.updatetData(params), function(err, results) {
            //todo mysql错误
            // if (util.isEmpty(results)) {
            //     if (!util.isEmpty(results)) {
            //         res.send({ "success": true, "data": {}, "msg": "更新成功", "code":200  });y
            //     } else {
            //         res.send({ "success": false, "data": {}, "msg": "参数输入不合法", "code":400  });
            //     }
            //     connection.release();
            // } else {
            //     res.send({ "success": false, "data": {}, "msg": "更新失败", "code":400  });
            // }
        });
    });
    res.send({ "success": true, "data": {}, "msg": "更新成功", "code":200 });
});

module.exports = router;